<?php
namespace app\blog\controller;
use think\Db;

class Home extends Base {

    public function slideList() {
        $where = [
            ['del','=',0],
            ['type','=',1]
        ];
        try {
            $list = Db::table('mp_slideshow')->where($where)->limit(0,9)->select();
        } catch (\Exception $e) {
            return ajax($e->getMessage(), -1);
        }
        return ajax($list);
    }

    public function newsList() {
        $curr_page = input('post.pageIndex',1);
        $perpage = input('post.pageSize',10);
        try {
            $whereCourse = [];
            $course_count = Db::table('mp_game')->where($whereCourse)->count();
            $course_list = Db::table('mp_game')
                ->where($whereCourse)
                ->order(['sort'=>'ASC','id'=>'DESC'])
                ->field('id,title,pic,create_time,admin_id')
                ->limit(($curr_page-1)*$perpage,$perpage)
                ->select();
        } catch (\Exception $e) {
            return ajax($e->getMessage(), -1);
        }
        $data['count'] = $course_count;
        $data['list'] = $course_list;
        return ajax($data);
    }

    public function newsDetail() {
        $where = [
            ['c.id','=',input('param.id')]
        ];
        try {
            $info = Db::table('mp_game')->alias('c')
                ->join('mp_admin a','c.admin_id=a.id','left')
                ->where($where)
                ->field('c.*,a.realname AS author')
                ->find();
            Db::table('mp_game')->alias('c')->where($where)->setInc('views',1);
        } catch (\Exception $e) {
            return ajax($e->getMessage(), -1);
        }
        $info['content'] = $this->ueditorAddPrefix($info['content'],true);
        $this->assign('cmd',$this->cmd);
        $this->assign('info',$info);
        return $this->fetch();
    }

    public function articleList()
    {
        $curr_page = input('post.pageIndex',1);
        $perpage = input('post.pageSize',10);
        $whereArticle = [
            ['a.status','=',1]
        ];
        try {
            $count = Db::table('mp_article')->alias('a')->where($whereArticle)->count();
            $list = Db::table('mp_article')->alias('a')
                ->join('mp_article_cate t','a.cate_id=t.id','left')
                ->where($whereArticle)
                ->field('a.id,a.title,a.pic,t.cate_name')
                ->order(['a.id'=>'DESC'])
                ->limit(($curr_page-1)*$perpage,$perpage)
                ->select();
        } catch (\Exception $e) {
            return ajax($e->getMessage(), -1);
        }
        return ajax([
            'count' => $count,
            'list' => $list,
        ]);
    }

    public function articleDetail()
    {
        $article_id = input('param.article_id',10);
        $whereArticle = [
            ['a.id','=',$article_id]
        ];
        try {
            $info = Db::table('mp_article')->alias('a')
                ->join('mp_article_cate c','a.cate_id=c.id','left')
                ->where($whereArticle)
                ->field('a.id,a.title,a.pic,a.content,a.create_time,c.cate_name')
                ->find();
            if(!$info) {
                return ajax('',-4);
            }
        } catch (\Exception $e) {
            return ajax($e->getMessage(), -1);
        }
        return ajax($info);
    }

    //检查是否绑定手机号, 并验证token时间, 如果快过期了可以更新
    public function refreshToken() {
        try {
            $time = time();
            $new_token = $this->userinfo['token'];
            if((time() - $this->userinfo['create_time']) > config('token_expires_time')/2) {
                $new_token = md5($this->userinfo['token'] . $time);
                $update = [
                    'create_time' => $time,
                    'token' => $new_token
                ];
                Db::table('mp_mix_token')->where('token','=',$this->userinfo['token'])->update($update);
            }
        } catch (\Exception $e) {
            mylog($this->cmd,$e->getMessage());
            return ajax($e->getMessage(),-1);
        }
        $ret = [
            'token' => $new_token
        ];
        return ajax($ret);

    }



}